﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Helper.Enumerators;
using System.Configuration;
using Prodma.DataAccess.Models;
using System.Web.Security;

namespace Prodma.DataAccess.DataService
{
    public class UserService 
    {

        private DataContext context = new DataContext();


        #region IRecordOperation<tb_User,UserVm> Members

        public LoginUser LogOn(string userName, string password)
        {
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5").ToLower();
            DataContext context = new DataContext();
            using (context)
            {
                var loginUser = (from user in context.tb_User
                                 where user.Password == password && user.UserName == userName
                                 select new LoginUser
                                 {
                                     Id = user.ID,
                                     UserName = user.UserName,
                                     FullName = user.FirstName + " " + user.LastName,
                                     DateTime = DateTime.Now,
                                     Role = new RoleVm { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search, ClearChart = user.tb_Role.ClearChart },
                                     CariKod = user.CARI.KOD,
                                     Unvan = user.CARI.AD,
                                     IsOnline = user.IsOnline,
                                     CariID = user.CariID
                                 }
                                 ).FirstOrDefault();


                return loginUser;
            }
        }
        public IList<UserVm> GetList()
        {
            using (context)
            {
                var list = (from user in context.tb_User
                            select new UserVm()
                            {
                                ID = user.RoleID,
                                Cari = new CariVm{ID = user.CARI.ID},
                                Role = new RoleVm{ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search},
                                UserName = user.UserName,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                LastName = user.LastName,
                                Mail = user.Mail,
                                MacAdres = user.MacAdres,
                                IpAdres = user.IpAdres,
                                IsOnline = user.IsOnline
                            }).OrderBy(x => x.FirstName).ToList();

                return list;
            }
        }
        public UserVm GetUser(int id) {
            using (context)
            {
                var list = (from user in context.tb_User where user.ID == id
                            select new UserVm()
                            {
                                ID = user.ID,
                                Cari = new CariVm { ID = user.CARI.ID },
                                Role = new RoleVm { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search },
                                UserName = user.UserName,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                LastName = user.LastName,
                                Mail = user.Mail,
                                MacAdres = user.MacAdres,
                                IpAdres = user.IpAdres,
                                IsOnline = user.IsOnline
                            }).First();

                return list;
            }
        }
        public void New(tb_User entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_User(entity);
                context.SaveChanges();
            }
        }
        public UserVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from user in context.tb_User
                            where user.ID == id
                            select new UserVm()
                            {
                                ID = user.ID,
                                Cari = new CariVm { ID = user.CARI.ID },
                                Role = new RoleVm { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search },
                                UserName = user.UserName,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                LastName = user.LastName,
                                Mail = user.Mail,
                                MacAdres = user.MacAdres,
                                IpAdres = user.IpAdres,
                                IsOnline = user.IsOnline
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_User entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from user in context.tb_User
                            where user.ID == id
                            select user).FirstOrDefault();

                edit.FirstName = entity.FirstName;
                edit.LastName = entity.LastName;
                edit.Mail = entity.Mail;
                edit.tb_RoleReference.EntityKey = entity.tb_RoleReference.EntityKey;
                if (id != 1)
                {
                    edit.LK_DURUMReference.EntityKey = entity.LK_DURUMReference.EntityKey;
                }

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            if (id == 1)
            {
                return;
            }
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from user in context.tb_User
                              where user.ID == id
                              select user).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }


        public String setOnlinorOfline(int id, Boolean status)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var currentUser = (from user in context.tb_User
                              where user.ID == id
                              select user).FirstOrDefault();

                currentUser.IsOnline = status;
                context.SaveChanges();
            }
            return "Success";
        }
        #endregion

        #region IPageOperation<UserVm> Members

        public IList<UserVm> GetList(int page)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from user in context.tb_User
                            select new UserVm()
                            {
                                ID = user.ID,
                                Cari = new CariVm { ID = user.CARI.ID },
                                Role = new RoleVm { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search },
                                UserName = user.UserName,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                LastName = user.LastName,
                                Mail = user.Mail,
                                MacAdres = user.MacAdres,
                                IpAdres = user.IpAdres,
                                IsOnline = user.IsOnline
                            }).OrderBy(x => x.FirstName).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        #endregion

        #region IPageableFindOperation<UserVm> Members

        public IList<UserVm> GetList(int page, Dictionary<string, string> find)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from user in context.tb_User
                            where user.FirstName.StartsWith(findName)
                            select new UserVm()
                            {
                                ID = user.ID,
                                Cari = new CariVm { ID = user.CARI.ID },
                                Role = new RoleVm { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search },
                                UserName = user.UserName,
                                Password = user.Password,
                                FirstName = user.FirstName,
                                LastName = user.LastName,
                                Mail = user.Mail,
                                MacAdres = user.MacAdres,
                                IpAdres = user.IpAdres,
                                IsOnline = user.IsOnline
                            }).OrderBy(x => x.FirstName).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        #endregion

        #region IPersonelOperation<tb_User> Members

        public void ChangeUserNameAndPassword(int id, tb_User entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var userData = (from user in context.tb_User
                                where user.ID == id
                                select user).FirstOrDefault();

                userData.UserName = entity.UserName;
                userData.Password = entity.Password;

                context.SaveChanges();
            }
        }

        public void ChangeUserProfile(int id, tb_User entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var userData = (from user in context.tb_User
                                where user.ID == id
                                select user).FirstOrDefault();

                userData.UserName = entity.UserName;
                userData.Password = entity.Password;
                userData.FirstName = entity.FirstName;
                userData.LastName = entity.LastName;
                userData.Mail = entity.Mail;

                context.SaveChanges();
            }
        }

        #endregion
        
        #region IRowCountOperation Members

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from user in context.tb_User
                                select user).Count();

                return rowCount;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from user in context.tb_User
                                where user.FirstName.StartsWith(findName)
                                select user).Count();

                return rowCount;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from user in context.tb_User
                                 where user.ID == id
                                 select user).FirstOrDefault();

                viewState.LK_DURUMReference.EntityKey = new EntityKey("DataContext.LK_DURUM", "ID", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from user in context.tb_User
                                 where user.ID == id
                                 select user).FirstOrDefault();

                viewState.LK_DURUMReference.EntityKey = new EntityKey("DataContext.LK_DURUM", "ID", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

        #region IImageOperation Members

        //public void RemoveImage(int id)
        //{
        //    DataContext context = new DataContext();
        //    using (context)
        //    {
        //        var removeImage = (from user in context.tb_User
        //                           where user.ID == id
        //                           select user).FirstOrDefault();

        //        removeImage.Photo = ConfigurationManager.AppSettings["userBlankImage"].ToString();

        //        context.SaveChanges();
        //    }
        //}
        #endregion

    }
}
